import { AppDispatch } from '@/store';
import http from '@/utils/request';
import { ProfileType } from '../reducer/user';

// 用户个人信息
export const getUserInfo = () => {
  return async (dispatch: AppDispatch) => {
    const res = await http.get('/v1_0/user');
    dispatch({ type: 'user/saveUser', payload: res.data });
  };
};

// 用户资料
export const getUserProfile = () => {
  return async (dispatch: AppDispatch) => {
    const res = await http.get(`/v1_0/user/profile`);
    dispatch({ type: 'user/saveProfile', payload: res.data });
  };
};

// 编辑头像
// export const updataPhotoApi = (data: FormData) => {
//   return http({
//     url: '/v1_0/user/photo',
//     method: 'PATCH',
//     data,
//   });
// };
export const updataPhotoApi = (data: FormData) => {
  return async (dispatch: AppDispatch) => {
    const res = await http.patch('/v1_0/user/photo', data);
    dispatch({ type: 'user/updataProfile', payload: res.data });
  };
};

// 编辑用户数据
export const updataProfileAction = (data: Partial<ProfileType>) => {
  return async (dispatch: AppDispatch) => {
    await http.patch('/v1_0/user/profile', data);
    dispatch({ type: 'user/updataProfile', payload: data });
  };
};
